<?php

namespace backend\controllers;

use Yii;
use common\models\Channel;
use yii\data\Pagination;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Json;

/**
 * ChannelController implements the CRUD actions for Channel model.
 */
class ChannelController extends AdminController
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Channel models.
     * @return mixed
     */
    public function actionIndex()
    {
		$data  = Channel::find()->where('status >= 0');

		$pages = new Pagination([
			'totalCount' => $data->count(),
		]);
		$list = $data->offset($pages->offset)->orderBy('sort')->limit($pages->limit)->all();

		Yii::$app->view->params['titles'] = '导航管理';
		return $this->render('index', [
			'list'  => $list,
			'pages' => $pages
		]);
    }

    /**
     * Creates a new Channel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Channel();

        if ($model->load(Yii::$app->request->post()) && $model->add()) {
			Yii::$app->session->setFlash('success', '新增成功');
            return $this->redirect(['index']);
        } else {
			Yii::$app->view->params['titles'] = '新增导航';
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Channel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

			Yii::$app->session->setFlash('success', '修改成功');
            return $this->redirect(['index']);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Channel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete()
    {
		$id = trim($_REQUEST['id'], ',');
		if (!empty($id)) {

			$result = Channel::updateAll(['status' => -1], "id in({$id})");

			if ($result) {
				Yii::$app->session->setFlash('success', '删除成功');

				if (Yii::$app->request->isAjax) {
					header('Content-type:application/json;charset=utf-8');
					exit(Json::encode(['ret' => '0']));
				}
			} else {
				Yii::$app->session->setFlash('error', '删除失败');

				if (Yii::$app->request->isAjax) {
					exit(Json::encode(['ret' => '1']));
				}
			}
		}
		return $this->redirect(['index']);
    }

    /**
     * Finds the Channel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return Channel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Channel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
